﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace outREACH.App_Code
{
    public class FinDonationHistory
    {
        public int transactionID { get; set; }
        public string userName { get; set; }
        public DateTime dateTime { get; set; }
        public string section { get; set; }
        public string country { get; set; }
        public int amount { get; set; }
        public string paymentType { get; set; }
        public DateTime date { get; set; }

        public static List<FinDonationHistory> getFinHistory(string userName)
        {
            SqlConnection conn = null;
            List<FinDonationHistory> result = new List<FinDonationHistory>();
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from TransactionHistory where userName = @userName";
                comm.Parameters.AddWithValue("@userName", userName);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    FinDonationHistory dh = new FinDonationHistory();
                    dh.amount = (int)dr["amount"];
                    dh.country = (string)dr["country"];
                    dh.date = (DateTime)dr["date"];
                    dh.dateTime = (DateTime)dr["dateTime"];
                    dh.paymentType = (string)dr["paymentType"];
                    dh.section = (string)dr["section"];
                    dh.transactionID = (int)dr["transactionID"];
                    result.Add(dh);
                }
                dr.Close();
                }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public static List<FinDonationHistory> getFinHistoryDate(string userName, DateTime date)
        {
            SqlConnection conn = null;
            List<FinDonationHistory> result = new List<FinDonationHistory>();
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from TransactionHistory where userName = @userName and date = @date";
                comm.Parameters.AddWithValue("@userName", userName);
                comm.Parameters.AddWithValue("@date", date);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    FinDonationHistory dh = new FinDonationHistory();
                    dh.amount = (int)dr["amount"];
                    dh.country = (string)dr["country"];
                    dh.dateTime = (DateTime)dr["dateTime"];
                    dh.paymentType = (string)dr["paymentType"];
                    dh.section = (string)dr["section"];
                    dh.transactionID = (int)dr["transactionID"];
                    result.Add(dh);
                }
                dr.Close();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public static void insertFinHistory(string userName, DateTime dateTime, string section, string country, int amount, string paymentType, DateTime date)
        {
            SqlConnection conn = null;
            int newtransactionID = 0;
            try
            {
                conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "insert into TransactionHistory(userName, dateTime, section, country, amount, paymentType, date) values (@userName, @dateTime, @section, @country, @amount, @paymentType, @date)";
                comm.Parameters.AddWithValue("@userName", userName);
                comm.Parameters.AddWithValue("@dateTime", dateTime);
                comm.Parameters.AddWithValue("@section", section);
                comm.Parameters.AddWithValue("@country", country);
                comm.Parameters.AddWithValue("@amount", amount);
                comm.Parameters.AddWithValue("@paymentType", paymentType);
                comm.Parameters.AddWithValue("@date", date);
                int rowsAffected = comm.ExecuteNonQuery();
                comm.CommandText = "select @@IDENTITY as NewtransactionID";
                SqlDataReader dr = comm.ExecuteReader();
                dr.Read();
                newtransactionID = int.Parse(dr["NewtransactionID"].ToString());
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

    }
}